package com.aishau.smartbj.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;


import com.aishau.smartbj.R;
import com.aishau.smartbj.utils.CacheUtils;

import java.util.ArrayList;

/**
 * Created by aishua on 2017/7/20.
 */
public class GuideActivity extends Activity {
    private static final String TAG = "GuideActivity-";
    private ViewPager vp_guide;
    private ArrayList<ImageView> imageViews;
    private LinearLayout ll_point_group;
    private Button btn_startmain;
    private ImageView img_red_point;
    private int leftMax;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);
        vp_guide = (ViewPager) findViewById(R.id.vp_guide);
        ll_point_group = (LinearLayout) findViewById(R.id.ll_point_group);
        btn_startmain = (Button) findViewById(R.id.btn_startmain);
        img_red_point = (ImageView) findViewById(R.id.img_red_point);
        int[] idS = {R.drawable.guide_1, R.drawable.guide_2, R.drawable.guide_3};
        imageViews = new ArrayList<>();
        for (int i = 0; i < idS.length; i++) {

            ImageView imageView = new ImageView(GuideActivity.this);
            imageView.setBackgroundResource(idS[i]);
            imageViews.add(imageView);

            ImageView imageIndiactor = new ImageView(GuideActivity.this);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(30, 30);
            if (i != 0) {
//                设置左边的margin
                layoutParams.leftMargin = 40;
            }
            imageIndiactor.setLayoutParams(layoutParams);
            imageIndiactor.setBackgroundResource(R.drawable.point_normal);
            ll_point_group.addView(imageIndiactor);
        }
        vp_guide.setAdapter(new MyAdapter());
        ll_point_group.getViewTreeObserver().addOnGlobalLayoutListener(new MyOnGlobalLayoutListener());
        vp_guide.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(30, 30);
                int leftmargin = (int) (leftMax * position + leftMax * positionOffset);
//                i = i+10;
                layoutParams.leftMargin = leftmargin;

                img_red_point.setLayoutParams(layoutParams);

            }

            @Override
            public void onPageSelected(int position) {

                if (position == vp_guide.getAdapter().getCount() - 1) {

                    btn_startmain.setVisibility(View.VISIBLE);
                } else {
                    btn_startmain.setVisibility(View.GONE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
        btn_startmain.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

//               进入引导页面
                Intent intent = new Intent(GuideActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
                CacheUtils.setBoolean(GuideActivity.this, SplashActivity.START_MAIN);

            }
        });
    }
    public class MyAdapter extends PagerAdapter {

        @Override
        public int getCount() {

            return imageViews.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {

            return view == object;
        }
        @Override
        public Object instantiateItem(ViewGroup container, int position) {

            View view = imageViews.get(position);
            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {

            container.removeView((View) object);
        }

    }

    public class MyOnGlobalLayoutListener implements ViewTreeObserver.OnGlobalLayoutListener {

        @Override
        public void onGlobalLayout() {

            ll_point_group.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            leftMax = ll_point_group.getChildAt(1).getLeft() - ll_point_group.getChildAt(0).getLeft();

        }
    }
}
